<script setup lang='ts'>
import { ref, watch } from 'vue'
import { messageBox, Message } from "earthsdk-ui"
const props = withDefaults(defineProps<{
    modelValue?: number[],
    readonly?: boolean,
    propertyName?: string
}>(), { withUndefined: true, propertyName: '属性值', readonly: false })
const emits = defineEmits(["update:modelValue"]);
const numbersRef = ref<any[]>([])
//时间戳转换为时间
const timestampToTime = (timestamp: number) => {
    const date = new Date(timestamp); // 根据时间戳创建Date对象
    const year = date.getFullYear(); // 获取年份
    const month = date.getMonth() + 1; // 获取月份，需要加1
    const day = date.getDate(); // 获取日期
    const hour = date.getHours(); // 获取小时
    const minute = date.getMinutes(); // 获取分钟
    const second = date.getSeconds(); // 获取秒数
    const millisecond = date.getMilliseconds(); // 获取秒数
    const formattedDate = `${year}-${month.toString().length === 1 ? `0${month}` : month}-${day.toString().length === 1 ? `0${day}` : day} ${hour.toString().length === 1 ? `0${hour}` : hour}:${minute.toString().length === 1 ? `0${minute}` : minute}:${second.toString().length === 1 ? `0${second}` : second}.${millisecond}`; // 拼接成格式化后的日期字符串
    return formattedDate
}
//时间转换为时间戳
const toTimetampTimes = (dateTime: string) => {
    const date = new Date(dateTime)
    const time = date.getTime()
    return time
}
//时间戳数组转换为时间
const timestampToTimeArray = (arr: number[]) => {
    return arr.map(item => {
        return timestampToTime(item)
    })
}
//时间数组转换为时间戳
const toTimetampTimesArray = (arr: string[]) => {
    return arr.map(item => {
        return toTimetampTimes(item)
    })
}

watch(() => props.modelValue, () => {
    const arr = JSON.parse(JSON.stringify(props.modelValue ?? [])) as number[]
    numbersRef.value = timestampToTimeArray(arr)
}, { immediate: true })

const addNumber = () => {
    numbersRef.value.push(timestampToTime(0))
    const arr = JSON.parse(JSON.stringify(numbersRef.value))
    const newNumsArr = toTimetampTimesArray(arr)
    emits('update:modelValue', newNumsArr)
}
const deleteNumber = (index: number) => {
    messageBox({ text: '确认删除此时间点吗?' }).then(() => {
        numbersRef.value.splice(index, 1)
        const arr = JSON.parse(JSON.stringify(numbersRef.value))
        const newNumsArr = toTimetampTimesArray(arr)
        emits('update:modelValue', newNumsArr)
        Message.success('删除成功')
    }).catch(() => {
     })

}

const inputHandler = (e: any, index: number) => {
    if (!numbersRef.value) return
    const numsArr = JSON.parse(JSON.stringify(numbersRef.value))
    numsArr[index] = e.target.value
    numbersRef.value = numsArr
    const arr = JSON.parse(JSON.stringify(numbersRef.value))
    const newNumsArr = toTimetampTimesArray(arr)
    emits('update:modelValue', newNumsArr)
}
const hoverIndex = ref(-2)
</script>

<template>
    <div class="prop_list">
        <div class="value_box_top">
            <span>时间序列</span>
            <span style="cursor: pointer;" @click="addNumber()" title="添加" @mouseenter="hoverIndex = -1"
                @mouseleave="hoverIndex = -2"><es-icon :name="'tianjia'" :color="hoverIndex === -1 ? '#fff' : '#575B66'"
                    :size="13" /></span>
        </div>
        <div class="value_box_content">
            <div class="value_box" v-for="(_, index) in numbersRef" :key="index">
                <input type="datetime-local" name="time" step=".1" :disabled="readonly" v-model="numbersRef[index]"
                    @input="inputHandler($event, index)">
                <span @click="deleteNumber(index)" title="删除" @mouseenter="hoverIndex = index"
                    @mouseleave="hoverIndex = -2"><es-icon :name="'shanchu_2'"
                        :color="hoverIndex === index ? '#fff' : '#575B66'" :size="13" /></span>
            </div>
        </div>
    </div>
</template>

<style scoped>
.prop_list {
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
}

.value_box_top {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-right: 6px;
    box-sizing: border-box;
    color: rgba(230, 230, 230, 1);
}

.value_box_content {
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    max-height: 130px;
    overflow: auto;
}

.value_box_content::-webkit-scrollbar {
    width: 3px;
    background-color: rgba(41, 42, 46, 1);
    border-radius: 2px;
}

.value_box_content::-webkit-scrollbar-thumb {
    background-color: rgba(183, 183, 183, 1);
    border-radius: 2px;
}

.value_box {
    width: 100%;
    display: flex;
    justify-content: space-between;
    margin: 5px 0px;
    align-items: center;

}

.value_box>input {
    width: 220px;
    height: 20px;
    border: 1px solid #3B3C40;
    outline: none;
    background: rgba(28, 28, 29, 0.6);
    color: rgba(230, 230, 230, 1);
    border-radius: 4px;

}

.value_box>span {
    width: 20px;
    cursor: pointer;
}
</style>
